import LocalStorage from "../Utils/LocalStorage";
import isNumber = cc.js.isNumber;

export default class UserProfile {
    //用户标识
    token: string = ""

    userId: string = ""

    user: string = ""

    //当前关卡
    currentLevel: number = 1
    //当前金币数
    coins: number = 0
    //最高分
    maxGoal: number = 0

    //总积分, 失败时清0
    score: number = 0

    //当前选择枪支.
    currentGun: number = 0
    //当前皮肤
    currentSkin: number = 0

    //音乐开关
    musicOpen: boolean = true
    //音效开关
    effectOpen: boolean = true

    //已签到多少天.
    signedDays: number = 0

    //收集的总金币数
    collectedCoins: number = 0

    //爆头数量
    headShoots: number = 0

    //打出的子弹数.
    shootBullets: number = 0

    //当前通缉令被杀了几人.
    blacklist_progress: number = 0

    //通关时领奖的序号.
    blacklistAward: number = 0

    //通关时特殊奖励的枪ID.
    blacklistSpecalAward: number = null

    //上一次通关后时否领取了奖励.
    hasClaimedAward: boolean = true

    //旧黑名单
    lastBlackList: Array<number> = []

    //当前黑名单.
    blacklist: Array<number> = []
    //解锁枪支Id
    unlockGuns: Array<number> = []

    //解锁的新枪支,显示枪库时用.
    unlockNewGuns: Array<number> = []
    //解锁新时装,显示新时装时用.
    unlockNewSkin: number[] = []

    //解锁时装ID
    unlockSkins: Array<number> = []

    //上一关的stageId.
    lastStageId: number = 0

    //生命数量.
    lives: number = 0

    //金币红包的时间信息记录
    lastRedPacketTime: { ts: number, counter: number, id: number, lastPacketId: number } = {
        ts: 0,
        counter: -1,
        id: 0,
        lastPacketId: 0
    }

    //是否已使用复活币.
    revived: boolean = false

    todayMaxScore: { score: number, ts: number } = {ts: 0, score: 0}

    //userInfo
    userInfo: { nickName?: string, avatarUrl?: string, gender?: string, city?: string, province?: string, country?: string, language?: string } = null
    //wx
    openId: string = null

    private static _prifle: UserProfile = null

//获取当前用户值
    static getProfile(): UserProfile {
        if (this._prifle) return this._prifle
        let profile = LocalStorage.getObj<UserProfile>("user_profile")
        if (!profile) {
            profile = new UserProfile()
        } else {
            profile.userId = profile.userId || ""
            profile.unlockGuns = profile.unlockGuns || []
            profile.unlockNewGuns = profile.unlockNewGuns || []
            profile.unlockSkins = profile.unlockSkins || []
            profile.lastBlackList = profile.lastBlackList || []
            profile.blacklist = profile.blacklist || []
            profile.unlockNewSkin = profile.unlockNewSkin || []
            profile.shootBullets = profile.shootBullets || 0
            profile.headShoots = profile.headShoots || 0
            profile.score = profile.score || 0
            profile.userInfo = profile.userInfo || {}
            profile.openId = profile.openId || ""
            profile.todayMaxScore = profile.todayMaxScore || {ts: 0, score: 0}
            profile.lastStageId = profile.lastStageId || 0
            profile.lives = profile.lives || 0
            profile.revived = profile.revived || false
            profile.lastRedPacketTime = profile.lastRedPacketTime || {ts: 0, counter: -1, id: 0, lastPacketId: 0}
        }
        this._prifle = profile
        return profile
    }

    //保存更新值
    static save(profile: UserProfile) {
        this._prifle = profile
        LocalStorage.setObj("user_profile", profile)
    }

    static addCoins(profile: UserProfile, coins: number | string) {
        if (isNumber(coins)) {
            profile.coins += (coins as number)
        } else {
            console.warn("获得的金币不是数字")
            profile.coins += parseInt(coins as string)
        }
    }

    static setTodayMaxScore(score: number) {
        let profile = UserProfile.getProfile()

        let lastDay = new Date(profile.todayMaxScore.ts)
        lastDay.setHours(0, 0, 0)
        let now = new Date()
        now.setHours(0, 0, 0)

        //相差>=1天时清零
        if (now.getTime() - lastDay.getTime() >= 86395000) {
            profile.todayMaxScore.score = score
            profile.todayMaxScore.ts = now.getTime()
        } else {
            if (profile.todayMaxScore.score < score) {
                profile.todayMaxScore.score = score
                profile.todayMaxScore.ts = now.getTime()
            }
        }
        UserProfile.save(profile)
    }

    static checkClearTodayMaxScore() {
        let profile = UserProfile.getProfile()
        let lastDay = new Date(profile.todayMaxScore.ts)
        lastDay.setHours(0, 0, 0)
        let now = new Date()
        now.setHours(0, 0, 0)
        // 24 * 60 * 60000-5000 ,提前5秒钟清零.
        if (now.getTime() - lastDay.getTime() >= 86395000) {
            profile.todayMaxScore.score = 0
            profile.todayMaxScore.ts = Date.now()
        }
        UserProfile.save(profile)
    }
}